package com.zmn.oms.business.impl.purpose;

import com.google.common.collect.Lists;
import com.zmn.base.channel.common.dro.ChannelDRO;
import com.zmn.base.channel.dubbo.interfaces.channel.ChannelListRemoteService;
import com.zmn.base.common.data.common.dro.area.AreaDRO;
import com.zmn.base.common.data.dubbo.interfaces.area.AreaListRemoteService;
import com.zmn.base.product.common.dto.categ.CategServDRO;
import com.zmn.base.product.common.dto.product.ProductBaseQuery;
import com.zmn.base.product.common.dto.product.ec.ProductBaseDRO;
import com.zmn.base.product.common.query.categ.ForeignCategServQuery;
import com.zmn.base.product.dubbo.interfaces.categ.serv.CategServListRemoteService;
import com.zmn.base.product.dubbo.interfaces.product.foreign.ProductForeignListRemoteService;
import com.zmn.brs.common.enums.EventEnum;
import com.zmn.brs.common.enums.RecordFieldEnum;
import com.zmn.brs.starter.annotation.RecordField;
import com.zmn.brs.starter.annotation.StaffOperateRecord;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.collection.CollectionUtil;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.purpose.OrderPurposeBService;
import com.zmn.oms.model.entity.purpose.OrderPurpose;
import com.zmn.oms.model.entity.purpose.OrderPurposeQuery;
import com.zmn.oms.model.vo.purpose.OrderPurposeVO;
import com.zmn.oms.services.interfaces.purpose.OrderPurposeService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 类描述：意向单
 *
 * @author liuying
 * @date 2018/11/06 11:41
 */
@Service
public class OrderPurposeBServiceImpl implements OrderPurposeBService {

    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private ProductForeignListRemoteService productForeignListRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private CategServListRemoteService categServListRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private ChannelListRemoteService channelListRemoteService;
    @Reference(version = com.zmn.base.plat.engine.common.constant.DubboConsts.INTERFACE_VERSION , check = false)
    private AreaListRemoteService areaListRemoteService;

    @Resource
    private OrderPurposeService orderPurposeService;

    @StaffOperateRecord(staffId = "{{#query.operatorId}}",
            event = EventEnum.WORK_ORDER_QUERY,
            mapping = {
            @RecordField(field = RecordFieldEnum.STAFF_IP, value = "{{#query.operatorIP}}"),
            @RecordField(field = RecordFieldEnum.PLAT, value = "10")
    })
    @Override
    public List<OrderPurposeVO> listOrderPurposeByQuery(OrderPurposeQuery query) {
      return baseListOrderPurposeByQuery(query);
    }

    @StaffOperateRecord(staffId = "{{#query.operatorId}}",
            event = EventEnum.WORK_ORDER_EXPORT,
            mapping = {
            @RecordField(field = RecordFieldEnum.STAFF_IP, value = "{{#query.operatorIP}}"),
            @RecordField(field = RecordFieldEnum.PLAT, value = "10")
    })
    @Override
    public List<OrderPurposeVO>  exportListOrderPurposeByQuery(OrderPurposeQuery query){
        return baseListOrderPurposeByQuery(query);
    }

    private List<OrderPurposeVO> baseListOrderPurposeByQuery(OrderPurposeQuery query) {
        List<OrderPurpose> orderPurposeList = orderPurposeService.listOrderPurpose(query);
        List<OrderPurposeVO> orderPurposeVOList = Lists.newArrayList();

        // servCategory  id,name集合
        List<CategServDRO> servCategoryDROS = categServListRemoteService.listByQuery(new ForeignCategServQuery()).getData();
        Map<Integer, String> servCategoryMap = servCategoryDROS.stream().collect(Collectors.toMap(CategServDRO::getServCategId, CategServDRO::getServCategName, (key1, key2) -> key2));

        orderPurposeList.forEach(item -> {
            OrderPurposeVO vo = BeanMapper.map(item, OrderPurposeVO.class);
            vo.setUsername(item.getUserName());
            vo.setReceiveTime(item.getCreateTime());
            vo.setUserPhone(item.getTelephone());
            vo.setUserStreet(item.getStreet());
            vo.setUserAddress(item.getAddress());
            // 设置服务分类
            if (item.getServCategId() != null) {
                vo.setServCategName(servCategoryMap.get(item.getServCategId()));
            }
            orderPurposeVOList.add(vo);
        });
        return orderPurposeVOList;
    }

    @Override
    public void insertOrderPurpose(OrderPurpose orderPurpose) {
        if (orderPurpose.getCityId() == null) {
            orderPurposeService.insertOrderPurpose(orderPurpose);
            return;
        }

        // 查询城市信息
        ResponseDTO<AreaDRO> cityResponseDTO = areaListRemoteService.getById(orderPurpose.getCityId());
        if (cityResponseDTO.getData() != null) {
            orderPurpose.setCityName(cityResponseDTO.getData().getName());

            // 查询省份信息
            ResponseDTO<AreaDRO> provinceResponseDTO = areaListRemoteService.getById(cityResponseDTO.getData().getParentId());
            if (provinceResponseDTO.getData() != null) {
                orderPurpose.setProvinceId(provinceResponseDTO.getData().getAreaId());
                orderPurpose.setProvinceName(provinceResponseDTO.getData().getName());
            }
        }

        // 产品
        if (orderPurpose.getProductId() != null) {
            ProductBaseQuery productDIO = new ProductBaseQuery();
            productDIO.setProductId(orderPurpose.getProductId());
            com.zmn.common.dto2.ResponseDTO<List<ProductBaseDRO>> productResponseDTO = productForeignListRemoteService.listBaseDROByQuery(productDIO);
            if (CollectionUtil.isNotNullOrEmpty(productResponseDTO.getData())) {
                ProductBaseDRO productBaseDRO = productResponseDTO.getData().get(0);
                orderPurpose.setProductInfo(productBaseDRO.getProductName());
                orderPurpose.setServCategId(productBaseDRO.getServCategId());
                orderPurpose.setCategId(productBaseDRO.getCategId());
                orderPurpose.setCategOneId(productBaseDRO.getCategOneId());
            }
        }

        // 查询区县信息
        if (orderPurpose.getCountyId() != null) {
            ResponseDTO<AreaDRO> countyResponseDTO = areaListRemoteService.getById(orderPurpose.getCountyId());
            if (countyResponseDTO.getData() != null) {
                orderPurpose.setProvinceId(countyResponseDTO.getData().getAreaId());
                orderPurpose.setProvinceName(countyResponseDTO.getData().getName());
            }
        }

        if (orderPurpose.getChannelId() != null) {
            ResponseDTO<ChannelDRO> channelDROResponseDTO = channelListRemoteService.getByChannelId(orderPurpose.getChannelId());
            if (channelDROResponseDTO.getData() != null) {
                orderPurpose.setChannelName(channelDROResponseDTO.getData().getName());
            }
        }

        String telephone = orderPurpose.getTelephone();
        if (StringUtils.isNotBlank(telephone) && telephone.length() >= 4) {
            orderPurpose.setTelephoneEnd4(telephone.substring(telephone.length() - 4));
        }

        orderPurpose.setCreateTime(DateUtil.getNow());
        orderPurposeService.insertOrderPurpose(orderPurpose);
    }

    @Override
    public void updateOrderPurpose(Integer orderPurposeId) {
        OrderPurpose orderPurpose = new OrderPurpose();
        orderPurpose.setOrderPurposeId(orderPurposeId);
        orderPurpose.setSaleStatus(GlobalConsts.YES);
        orderPurposeService.updateOrderPurpose(orderPurpose);
    }

    @Override
    public OrderPurpose findOrderPurposeByKey(Integer orderPurposeId) {
        return orderPurposeService.findOrderPurposeByKey(orderPurposeId);
    }

    @Override
    public void updateIsCreateOrder(Integer orderPurposeId) {
        OrderPurpose orderPurpose = new OrderPurpose();
        orderPurpose.setOrderPurposeId(orderPurposeId);
        orderPurpose.setIsCreateOrder(GlobalConsts.YES);
        orderPurposeService.updateOrderPurpose(orderPurpose);
    }
}
